﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using Iudicium.Persona;
using Iudicium.Persona.CRN;

public partial class UserControls_SelectPeople : System.Web.UI.UserControl
{
    private static readonly ILog log = LogManager.GetLogger("LogFile");

    #region Propiedades

    public int SelectedId
    {
        set { SelectedIdHiddenField.Value = value.ToString(); }
        get
        {
            int selectedId = 0;
            try
            {
                selectedId = Convert.ToInt32(SelectedIdHiddenField.Value);
            }
            catch(Exception ex)
            {
                log.Error("No se pudo convertir a entero el valor del control SelectedIdHiddenField", ex);
                selectedId = 0;
            }
            return selectedId;
        }
    }

    public string SelectedName
    {
        set { SelectedPerson.Text = value; }
        get { return SelectedPerson.Text; }
    }

    public int PersonaJuridicaId
    {
        get
        {
            int id = 0;
            try
            {
                id = Convert.ToInt32(PersonaJuridicaIdHiddenField.Value);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo convertir a entero el valor del control PersonaJuridicaIdHiddenField", ex);
                id = 0;
            }
            return id;
        }
        set { PersonaJuridicaIdHiddenField.Value = value.ToString(); }
    }

    public bool Enabled
    {
        set { OpenDialog.Visible = value; }
        get { return OpenDialog.Visible; }
    }

    public string SelectCommandLabel
    {
        set { OpenDialog.Text = value; }
        get { return OpenDialog.Text; }
    }

    protected string SearchRequestHandler
    {
        get { return VirtualPathUtility.ToAbsolute("~/UserControls/Handlers/SearchPeopleHandler.ashx"); }
    }

    public string RepresentantesInsertados
    {
        get { return InsertedPeopleHiddenField.Value; }
    }

    public string RepresentantesEliminados
    {
        get { return DeletedPeopleHiddenField.Value; }
    }

    #endregion

    public SelMode SelectionMode
    {
        set { SelectionModeHiddenField.Value = value.ToString(); }
        get 
        {
            return SelectionModeHiddenField.Value.Equals(SelMode.Representantes.ToString()) ?
                SelMode.Representantes : SelMode.Simple; 
        }
    }

    public ObjType ObjectType
    {
        set { ObjectTypeHiddenField.Value = value.ToString(); }
        get { return ObjectTypeHiddenField.Value.Equals("Administrativo") ? ObjType.Usuario : ObjType.Administrativo; }
    }

    public enum SelMode
    {
        Simple,
        Representantes
    }

    public enum ObjType
    {
        Usuario,
        Administrativo
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (SelectionMode == SelMode.Simple)
        {
            SelectedPeople.Visible = false;
            return;
        }
        else
        {
            SelectedPeople.Visible = true;
            SelectedPerson.Visible = false;
            if(SelectionMode == SelMode.Representantes)
            {
                List<Representante> list = null;
                try
                {
                    list = RepresentanteCRN.GetRepresentantesByPersonaJuridicaId(PersonaJuridicaId);
                }
                catch(Exception ex)
                {
                    log.Error("No se pudo obtener la lista de Personas Representantes para la persona juridica con id " + PersonaJuridicaId, ex);
                    list = new List<Representante>();
                }

                SelectedPeopleRepeater.DataSource = list;
                SelectedPeopleRepeater.DataBind();
            }
            try
            {
                Literal literal = (Literal)SelectedPeopleRepeater.FindControl("EmptyTemplateLiteral");
                if (literal != null)
                    literal.Visible = SelectedPeopleRepeater.Items.Count == 0;
            }
            catch (Exception ex)
            {
                log.Error("No se pudo determinar la visibilidad del emptyTemplate del Repeater de personas", ex);
            }
        }
        
    }

    protected string GetClassName()
    {
        return ClientID + "_removePerson";
    }

    protected bool IsRepeaterVisible()
    {
        return SelectedPeopleRepeater.Items.Count == 0;
    }

    public string GetValueHtml()
    {
        return "<input type='hidden' value='' name='" + UniqueID +"' id='" + ClientID + "' />";
    }
}
